home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13736 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: news2.cais.com!news
  2. From: Nehal Trivedi <ntrivedi@cais.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: What Should An Exception Handling Do? -- Clarification of rules
  5. Date: Tue, 26 Mar 1996 23:53:55 -0500
  6. Organization: Instant Karma
  7. Message-ID: <3158C9E3.35B8@cais.com>
  8. References: <1996Mar14.155641.4299@schbbs.mot.com> <4irn11$7ln@mimas.brunel.ac.uk> <Pine.Sola.3.91.960322041345.17711C-100000@ux5.cso.uiuc.edu> <slrn4lerdj.2be.srkumar@shell.monmouth.com>
  9. NNTP-Posting-Host: ntrivedi.cais.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Senthil Kumar wrote:
  16. > The way I look at exception handling (so far) is as a means of passing
  17. > error messages (which cannot be ignored) from one layer of code to a
  18. > higher layer of code, when possible errors cannot be resolved within
  19. > the layer that does the throw.
  20. > For eg. consider a function called fprintf, which takes a FILE *
  21. > as its first argument. Now obviously, fprintf cannot do a damn thing
  22. > with a NULL FILE *, and thus in this case it would be a good idea to
  23. > throw an exception, telling the caller, "look, you better check this out."
  24. > Am I on the right track? I would appreciate some comments, additional
  25. > refinements etc.
  26. > thanks,
  27. > Senthil
  28.  
  29. Think of exception handling as the way to handle errors. The advantage of using 
  30. exceptions instead of return codes is that using exceptions consistently and 
  31. properly allows for a clean distinction between normal logic (in the try block) 
  32. and the error-detection and handling logic (in the catch block). Also since 
  33. exceptions automatically propagate if no handled, each layer only has to define 
  34. catch blocks for the error-conditions that it knows how to recover from. Also 
  35. each layer can make a decision about what diagnostic action to take in response 
  36. to an error e.g. log the error to the system log. 
  37. Typically "harsh" decisions such as program termination should be handled by the 
  38. outermost catch block.
  39.  
  40. In my experience the best way to handle exceptions is to use a small but 
  41. well-defined hierarchy of exception classes that cover all the error types in 
  42. your system.
  43.  
  44. Nehal Trivedi
  45.